package com.power.sifa.supervise.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.supervise.api.IApl_superviseFeign;
import com.power.sifa.supervise.model.Apl_supervise;
import com.power.sifa.supervise.model.pageInfo.Apl_supervisePageInfo;
import com.power.sifa.supervise.service.Apl_superviseService;
import com.power.sifa.supervise.service.BusinessSuperviseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   复议监督主表控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_superviseController",tags={"复议监督主表"})
public class Apl_superviseController implements IApl_superviseFeign {

    @Resource
    private Apl_superviseService apl_superviseService;

    @Resource
    private BusinessSuperviseService businessSuperviseService;



    @Override
    @ApiOperation("查询复议监督主表分页列表")
    @SaCheckPermission("apl_supervise_list")
    public ResponseDTO list(Apl_supervisePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_supervise> page = this.apl_superviseService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_supervise apl_supervise : page) {
                apl_supervise.geneText();
                list.add(apl_supervise);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看复议监督主表")
    @SaCheckPermission("apl_supervise_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_supervise apl_supervise  = apl_superviseService.getById(id);
        if (apl_supervise != null) {
            apl_supervise.geneText();
        }
        return ResponseDTO.succData(apl_supervise);
    }

    @Override
    @ApiOperation("删除复议监督主表")
    @SaCheckPermission("apl_supervise_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_superviseService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改复议监督主表")
    @SaCheckPermission("apl_supervise_save")
    public ResponseDTO save(Apl_supervise apl_supervise) {
        Assert.notNull(apl_supervise, "apl_supervise不能为空");
        apl_superviseService.saveOrUpdate(apl_supervise);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除复议监督主表")
    @SaCheckPermission("apl_supervise_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_superviseService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_supervise_list")
    public void output(HttpServletResponse response, Apl_supervisePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "复议监督主表";
        String[] heads = {"applyid","申请人","被申请人","被申请人的行政区划","事项信息","事实依据","调查结果材料","决定类型","不予支持通知书","是否需要整改","案件承办人id","案件承办人name","案件协办人id","案件协办人name","反馈截止日期","案件来源"};
        String[] fields = {"id","applyname","targetname","targetadid","matinfo","facbasis","invesattach","decisiontype","notnotifi","isrectifica","firstid","firstname","secondid","secondname","enddate","source"};
        Page<Apl_supervise> page = this.apl_superviseService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "复议监督主表";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid","申请人","被申请人","被申请人的行政区划","事项信息","事实依据","调查结果材料","决定类型","不予支持通知书","是否需要整改","案件承办人id","案件承办人name","案件协办人id","案件协办人name","反馈截止日期","案件来源"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyname","targetname","targetadid","matinfo","facbasis","invesattach","decisiontype","notnotifi","isrectifica","firstid","firstname","secondid","secondname","enddate","source"};
        List<Apl_supervise> list = (List<Apl_supervise>)ExcelUtil.Import(fields, file, Apl_supervise.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_superviseService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO updateFirstAndSecondPerson(List<String> ids, String firstid, String firstname, String secondid, String secondname, String thirdid, String thirdname) {
        try {
            Assert.notNull(ids,"修改id不能为空");
            if (CollectionUtil.isNotEmpty(ids)) {
                this.apl_superviseService.updateFirstAndSecondPerson(ids, firstid, firstname, secondid, secondname, thirdid, thirdname);
                return ResponseDTO.succ();
            }
            return ResponseDTO.wrap(500, "更新第一/第二承办人信息失败,id 为空");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("updateFirstAndSecondPerson 更新第一/第二承办人信息是失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "更新第一/第二承办人信息失败");
        }
    }


    @Override
    public ResponseDTO resetFirstAndSecondPerson(List<String> ids) {
        try {
            Assert.notNull(ids,"修改id不能为空");
            if (CollectionUtil.isNotEmpty(ids)) {
                this.apl_superviseService.resetFirstAndSecondPerson(ids);
                return ResponseDTO.succ();
            }
            return ResponseDTO.wrap(500, "更新第一/第二承办人信息失败,id 为空");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("updateFirstAndSecondPerson 更新第一/第二承办人信息是失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "更新第一/第二承办人信息失败");
        }
    }



    public ResponseDTO sendTargetMessage(String applyid, String userid, List<String> phones) {
        Assert.notNull(phones, "联系号码不能为空");
        Assert.notNull(applyid, "applyid不能为空");
        Assert.notNull(userid, "userid不能为空");
        try {
            this.businessSuperviseService.sendDiTodo(applyid,userid, phones, Boolean.FALSE);
            return ResponseDTO.succMsg("发送成功");
//            return ResponseDTO.wrap(500, "发送赣政通待办功能暂未开放");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.wrap(500, "发送失败");
        }
    }

    @Override
    public ResponseDTO queryByPage(Apl_supervisePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        PageDataResponse result  = apl_superviseService.queryByPage(pageInfo);
        return ResponseDTO.succData(result);
    }

    @Override
    public ResponseDTO queryArchivePage(Apl_supervisePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        PageDataResponse result  = apl_superviseService.queryArchivePage(pageInfo);
        return ResponseDTO.succData(result);
    }


}
